/*
*
* DMode
*
* This class is meant to represent a drawing mode
*
*/
class DMode{
  
  // -----------------------------------------------
  // attributes
  // -----------------------------------------------
  String name, hashName;                            // drawing mode display name and name by which it's identified in NHood.metricHash
  float absMin, absMax, relMin, relMax, ave;        // absolute min/max, relative min/max, and average metric values
                                                    // "absolute" min/max refers to manually specified limits, "relative" to auto-calculated values
  int maxMode;                                      // specifies min/max mode; 0 is "relative", 1 is "absolute", 2 is "static relative"
                                                    // "Relative" means that a global maximum and minimum is calculated, and colour is assigned to the
                                                    // poll using these values. "Absolute" means that colour is assigned based on the values entered
                                                    // when the DMode is instantiated, regardless of minimums and maximums. "Static relative" means that
                                                    // the drawing mode can't be toggled to "absolute;" this is for metrics like "population" in which
                                                    // it may not make sense to have arbitrary "absolute" values specified
  ColourTable cTable;                               // metricColour table (from giCentre utilities; automatically calculates colour values
  boolean hasMetric;                                // descibes whether or not drawing mode has a metric to display
                                                    // (this will almost always be true, but not for "neighbourhood" display mode)
  int cTableType;                                   // integer indicating type of preset ColourTable to create
  
  // -----------------------------------------------
  // constructor
  // calculates metric absolute max, absolute min, and ave values
  // -----------------------------------------------
  DMode(String na, Poll[] polls, int ctt, String hna, boolean hm, float amin, float amax, int mm) {
    
    name = na;
    hashName = hna;
    cTableType = ctt;
    hasMetric = hm;
    absMin = amin;
    absMax = amax;
    maxMode = mm;
    // note about 
    
    // if drawing mode has a metric, calculate max, min, and ave
    if(hasMetric) {
      
      float[] metric = new float[polls.length];
      //int lim = hoods.length;
      int it = 0;
      float sum = 0;

      // iterate through neighbourhoods
      for(int i = 0; i < polls.length; i++) {

        // retrieve drawing mode's metric from NHood's metricHash
        metric[it] = (Float) polls[i].metricHash.get(hashName);
        
        
        // don't store element if it's flagged as having no data
        // if it does have data, add to sum
        if(metric[it] != -1) {
          sum += metric[it];
          it += 1; 
        } else {
          metric = shorten(metric);
        } // if
        
      } // for
      
      // store max, min, ave
      relMax = max(metric);
      relMin = min(metric);
      ave = sum/metric.length;

    } // if
    
    // set colour table using setCTable method
    if(maxMode == 0) {
      cTable = ColourTable.getPresetColourTable(cTableType, relMin, relMax);
    } else if(maxMode == 1) {
      cTable = ColourTable.getPresetColourTable(cTableType, absMin, absMax);
    } else if(maxMode == 2) {
      cTable = ColourTable.getPresetColourTable(cTableType, relMin, relMax);
    }
    
  } // constructor
  
  // -----------------------------------------------
  // getMin
  // returns current minimum value based on current maxMode
  // -----------------------------------------------
  float getMin() {
    
    if(maxMode == 0) {
      return relMin;
    } else if(maxMode == 1) {
      return absMin;
    } else if(maxMode == 2) {
      return relMin;
    } else {
      return 0;
    }
    
  } // getMin

  // -----------------------------------------------
  // getMax
  // returns current maximum value based on current maxMode
  // -----------------------------------------------
  float getMax() {
    
    if(maxMode == 0) {
      return relMax;
    } else if(maxMode == 1) {
      return absMax;
    } else if (maxMode == 2) {
      return relMax;
    } else {
      return 0;
    }
    
  } // getMin  
  
  // -----------------------------------------------
  // switchMMMode
  // changes maxMode from current value and updates colourTable
  // -----------------------------------------------
  void switchMMMode() {
    
    if(maxMode == 0) {
      maxMode = 1;
      cTable = ColourTable.getPresetColourTable(cTableType, absMin, absMax);
    } else if (maxMode == 1) {
      maxMode = 0;
      cTable = ColourTable.getPresetColourTable(cTableType, relMin, relMax);
    } 
    
  } // switchMMode
  
  // -----------------------------------------------
  // getColour
  // returns colour value, given input value
  // -----------------------------------------------
  color getColour(float val) {
    
    // retrive colour from ColourTable
    color out = cTable.findColour(val);
    
    // return colour
    return out;    
    
  } // getColour
  
} // DMode
